package com.service.product.impl;

import java.util.Date;
import java.util.List;

import com.dao.OrderDao;
import com.entity.Cart;
import com.entity.DetailProduct;
import com.entity.Order;
import com.entity.OrderDetail;
import com.entity.User;
import com.service.product.OrderService;
import com.utils.CartUtil;
import com.utils.MybatisUtil;
import com.utils.Pager;
import com.utils.StringUtils;

public class OrderServiceImpl implements OrderService{

	/**
	 * 生成订单
	 */
	@Override
	public Order payShoppingCart(CartUtil cart, User user, String address) {
		Order order = new Order();
		// 更新订单
		order.setUserId(user.getId() + "");
		order.setUserAddress(address);
		order.setCreateTime(new Date());
		order.setCost(cart.getTotalCost());
		order.setSerialNumber(StringUtils.randomUUID());
		order.setLoginName(user.getLoginName());
		this.finAddOrder(order);
		for (Cart item : cart.getList()) {
			// 更新订单详情
			OrderDetail detail = new OrderDetail();
			detail.setOrderId(order.getId());
			detail.setCost(item.getPrice());
			detail.setProductId(item.getProduct().getId());
			detail.setQuantity(item.getStock());
			this.finAddOrderDetail(detail);
			new ProductServiceImpl().finUpdatePro(item.getStock(), item.getProduct().getId());
		}
		return order;
	}

	/**
	 * 订单
	 */
	@Override
	public int finAddOrder(Order order) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		int addOrder = or.addOrder(order);
		MybatisUtil.closeCommit();
		return addOrder;
	}

	/**
	 * 订单详情
	 */
	@Override
	public int finAddOrderDetail(OrderDetail detail) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		int addOrder = or.addOrderDetail(detail);
		MybatisUtil.closeCommit();
		return addOrder;
	}

	/**
	 * 查询订单总数
	 */
	@Override
	public int finuserByIdOrder(int id) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		int addOrder = or.UserByIdOrder(id);
		MybatisUtil.closeCommit();
		return addOrder;
	}

	@Override
	public List<Order> finOrderList(int userid,Pager pager) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		List<Order> addOrder = or.orderList(userid,pager);
		MybatisUtil.closeCommit();
		return addOrder;
	}

	@Override
	public List<DetailProduct> finOrderDetail() {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		List<DetailProduct> addOrder = or.orderDetail();
		MybatisUtil.closeCommit();
		return addOrder;
	}

	@Override
	public int finOrderDetailCount() {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		int orderDetailCount = or.orderDetailCount();
		MybatisUtil.closeCommit();
		return orderDetailCount;
	}

	@Override
	public List<Order> finOrderListLike(String name, int userid, Pager pager) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		List<Order> orderListLike = or.orderListLike(name,userid,pager);
		MybatisUtil.closeCommit();
		return orderListLike;
	}

	@Override
	public int finOrderLikeCount(String name, int userid) {
		OrderDao or = MybatisUtil.getMapper(OrderDao.class);
		int orderLikeCount = or.orderLikeCount(name,userid);
		MybatisUtil.closeCommit();
		return orderLikeCount;
	}
	
	
	
}
